package com.spzx.user.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.ProductSkuFeignClient;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.user.api.domain.UpdateUserLogin;
import com.spzx.user.domain.UserAddress;
import com.spzx.user.domain.UserBrowseHistory;
import com.spzx.user.domain.UserCollect;
import com.spzx.user.mapper.UserAddressMapper;
import com.spzx.user.mapper.UserBrowseHistoryMapper;
import com.spzx.user.mapper.UserCollectMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.spzx.user.mapper.UserInfoMapper;
import com.spzx.user.api.domain.UserInfo;
import com.spzx.user.service.IUserInfoService;

/**
 * 会员信息Service业务层处理
 *
 * @author atguigu
 * @date 2024-08-03
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private UserAddressMapper userAddressMapper;
    @Resource
    private UserCollectMapper userCollectMapper;
    @Resource
    private UserBrowseHistoryMapper userBrowseHistoryMapper;
    @Resource
    private ProductSkuFeignClient productSkuFeignClient;


    /**
     * 查询会员信息列表
     *
     * @param userInfo 会员信息
     * @return 会员信息
     */
    @Override
    public List<UserInfo> selectUserInfoList(UserInfo userInfo) {
        return userInfoMapper.selectUserInfoList(userInfo);
    }

    /**
     * 获取用户地址信息
     *
     * @param userId 用户id
     * @return 用户地址信息列表
     */
    @Override
    public List<UserAddress> selectUserAddressList(Long userId) {
        return userAddressMapper.selectList(new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUserId, userId));
    }

    @Override
    public void register(UserInfo userInfo) {
        //先判断用户名是否存在
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getUsername, userInfo.getUsername());
        Long count = userInfoMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new ServiceException("该手机号注册的用户已存在");
        }
        userInfo.setAvatar("https://img0.baidu.com/it/u=4154833386,1880487480&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500");
        userInfo.setStatus(1);
        userInfo.setPhone(userInfo.getUsername());
        userInfoMapper.insert(userInfo);
    }

    @Override
    public Boolean updateUserLogin(UpdateUserLogin updateUserLogin) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(updateUserLogin.getUserId());
        userInfo.setLastLoginIp(updateUserLogin.getLastLoginIp());
        userInfo.setLastLoginTime(updateUserLogin.getLastLoginTime());
        userInfoMapper.updateById(userInfo);
        return true;
    }

    @Override
    public UserInfo selectUserByUserName(String username) {
        return userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUsername, username));
    }

    @Override
    public Boolean isCollect(Long skuId) {
        UserCollect userCollect = userCollectMapper.selectOne(new LambdaQueryWrapper<UserCollect>().eq(UserCollect::getSkuId, skuId));
        if (userCollect == null) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean collect(Long skuId) {
        UserCollect userCollect = new UserCollect();
        userCollect.setSkuId(skuId);
        userCollect.setUserId(SecurityContextHolder.getUserId());
        R<ProductSku> productSkuResult = productSkuFeignClient.getProductSku(skuId, SecurityConstants.INNER);
        if (productSkuResult.getCode() == R.FAIL) {
            throw new ServiceException(productSkuResult.getMsg());
        }
        ProductSku productSku = productSkuResult.getData();
        userCollect.setProductSku(productSku);
        userCollectMapper.insert(userCollect);
        return true;
    }

    @Override
    public Boolean cancelCollect(Long skuId) {
        userCollectMapper.delete(new LambdaQueryWrapper<UserCollect>().eq(UserCollect::getSkuId, skuId));
        return true;
    }

    /**
     * 用户收藏分页列表查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<UserCollect> userCollectList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserCollect> userCollectList = userCollectMapper.selectList(new LambdaQueryWrapper<UserCollect>().eq(UserCollect::getUserId, SecurityContextHolder.getUserId()));
        for (UserCollect userCollect : userCollectList) {
            R<ProductSku> productSkuResult = productSkuFeignClient.getProductSku(userCollect.getSkuId(), SecurityConstants.INNER);
            if (productSkuResult.getCode() == R.FAIL) {
                throw new ServiceException(productSkuResult.getMsg());
            }
            ProductSku productSku = productSkuResult.getData();
            userCollect.setProductSku(productSku);
        }
        return userCollectList;
    }

    /**
     * 商品浏览历史
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<UserBrowseHistory> userBrowseHistoryList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserBrowseHistory> userBrowseHistoryList = userBrowseHistoryMapper.selectList(new LambdaQueryWrapper<UserBrowseHistory>().eq(UserBrowseHistory::getUserId, SecurityContextHolder.getUserId()));
        return userBrowseHistoryList;
    }
}
